package com.ljl.service;

import com.alibaba.fastjson.JSONObject;
import com.ljl.entity.BodyAnalysisResponse;
import com.ljl.entity.BodyLocation;
import com.ljl.entity.BodyPart;
import com.ljl.entity.PersonInfo;
import com.ljl.util.ImageTools;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author 刘家良
 * @date 2022/10/30 20:32
 * @description /
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class KeyPointService {

    public void resolveImage(File input, List<PersonInfo> personInfos, File output) {
        try {
            BufferedImage image = ImageTools.getBufferedImage(input.getAbsolutePath());
            for (PersonInfo personInfo : personInfos) {
                //处理人物的位置信息
                BodyLocation location = personInfo.getLocation();
                resolveLocation(image, location);
                //处理身体部位
                Map<String, BodyPart> bodyParts = personInfo.getBody_parts();
                resolveBodyParts(image, bodyParts);
            }
            //输出图片
            ImageTools.saveBufferedImage(image, output.getAbsolutePath());
        }catch (Exception e){
            log.error("无法处理图片[{}]", input.getAbsolutePath());
        }
    }

    public void resolveImage(File input, PersonInfo personInfo, File output) {
        try {
            BufferedImage image = ImageTools.getBufferedImage(input.getAbsolutePath());
            //处理人物的位置信息
            BodyLocation location = personInfo.getLocation();
            resolveLocation(image, location);
            //处理身体部位
            Map<String, BodyPart> bodyParts = personInfo.getBody_parts();
            resolveBodyParts(image, bodyParts);
            //输出图片
            ImageTools.saveBufferedImage(image, output.getAbsolutePath());
        }catch (Exception e){
            log.error("无法处理图片[{}]", input.getAbsolutePath());
        }
    }

    private void resolveLocation(BufferedImage image, BodyLocation location) {
        //处理人物的位置信息
        int left = BigDecimal.valueOf(location.getLeft()).intValue();
        int top = BigDecimal.valueOf(location.getTop()).intValue();
        int width = BigDecimal.valueOf(location.getWidth()).intValue();
        int height = BigDecimal.valueOf(location.getHeight()).intValue();
        //使用红框，框出人物所在位置
        Graphics graphics = image.createGraphics();
        graphics.setColor(Color.RED);
        graphics.drawRect(left, top, width, height);
        graphics.dispose();
    }

    private void resolveBodyParts(BufferedImage image, Map<String, BodyPart> bodyParts) {
        //处理人物身体部位信息
        //使用红框，框出人物所在位置
        Graphics graphics = image.createGraphics();
        graphics.setColor(Color.cyan);
        Set<Map.Entry<String, BodyPart>> entries = bodyParts.entrySet();
        for (Map.Entry<String, BodyPart> entry : entries) {
            BodyPart value = entry.getValue();
            float score = value.getScore();
            //得分大于0.3才认为是人体身体部位
            if (score >= 0.3) {
                int x = BigDecimal.valueOf(value.getX()).intValue();
                int y = BigDecimal.valueOf(value.getY()).intValue();
                graphics.fillOval(x, y, 10, 10);
            }
        }
        graphics.dispose();
    }


}
